package org.algorithm.biubiu.wheel_1.数组.三月.第二周;

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @Date 2022/3/13 23:32
 * @Created by biubiu
 * @Link {https://leetcode-cn.com/problems/sort-colors/}
 */
public class 颜色分类 {


    @Test
    public  void test(){
        int[] nus = {1,2,0};
        sortColors(nus);
        System.out.println(Arrays.toString(nus));
    }

    public void sortColors(int[] nums) {
        int zero =0,  two = nums.length-1, index =0;
        while(index<nums.length){
            if(nums[index]==0){
                swap(nums,index,zero);
                zero++;
                index++;
            }else if(nums[index]==2 && index<two){
                swap(nums,index,two);
                two--;
            }else{
                index++;
            }


        }

    }

    public void swap (int[] nums,int n,int m){
        int tmp = nums[n];
        nums[n] = nums[m];
        nums[m] = tmp;
    }
}
